#include <iostream>
using std::cout;
using std::endl;

class A
{
public:
    virtual void print(){
        cout << "A print()" << '\n';
    }
    virtual void show(){
        cout << "A show()" << '\n';
    }
};

class B
{
public:
    virtual void print(){
        cout << "B print()" << '\n';
    }
    void show(){
        cout << "B show()" << '\n';
    }
    void display(){
        cout << "B display()" << '\n';
    }
};

class C
: public A
, public B
//: virtual public A
//, virtual public B
{
public:
    void print(){
        cout << "C print()" << '\n';
    }
//    void show(){
//        cout << "C show()" << '\n';
//    }
    void display(){
        cout << "C display()" << '\n';
    }
};

class D
: public C
{
public:
    void show(){
        cout << "D show()" << '\n';
    }
};

void test1(){
    D d;
    C* pc1 = &d;
    //pc1->show();    // C中没有show时，不能通过编译：member 'show' found in multiple base classes of different types
                    // 无法确定C中的show来自与A或者是B，此处涉及多重继承问题(注意不是菱形继承，所以虚继承是没有用的)
    
    pc1->A::show(); // A::show(); 输出不是D，此处强制静态绑定，没走多态
    pc1->B::show(); // B::show(); 输出不是D，此处强制静态绑定，没走多态
    //pc1->D::show(); // 编译失败，'D::show' is not a member of class 'C'

    B* pb1 = &d;
    pb1->show();    // B show(): C中没有show时，B::show()不是虚函数，静态绑定到B: show()
    
    A* pa1 = &d;    // D show(): C中没有show时，A走多态
    pa1->show();

    C c;
    B* pb2 = &c;    // B show(): C中没有show时，B::show()不是虚函数，静态绑定到B: show()
    pb2->show();

    A* pa2 = &c;    // A show(): C中没有show时，A输出自己
    pa2->show();
}

void test2(){
    D d;
    C* pc1 = &d;
    //pc1->show();    // D show()：C中有show时，C走多态被D重写
    
    B* pb1 = &d;
    pb1->show();    // B show(): C中有show时，B::show()非虚函数，始终静态绑定
    
    A* pa1 = &d;    // D show(): C中有show时，A::show()是虚函数且被D重写
    pa1->show();

    C c;
    B* pb2 = &c;    // B show(): C中有show时，静态绑定，无视C中的同名函数
    pb2->show();

    A* pa2 = &c;    // C show(): C中有show时，A::show()是虚函数且被C重写
    pa2->show();
}

int main()
{
    test1();
    //test2();
    
    return 0;
}

